package com.tust.controller;

import com.tust.domain.User;
import com.tust.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/login.do")
    public String login(String userName, String password) {
        List<User> user = userService.selectUserByUserName(userName);//查询用户名
        String msg = null;
        if (user.size() == 1) {//用户存在
            if (user.get(0).getPassword().equals(password)) {
                msg = "loginSuccess";
            } else {//密码错误
                msg = "passwordFalse";
            }
        } else {//用户不存在
            msg = "userUnExist";
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/register.do")
    public String register(String userName, String password) {
        String msg = null;
        List<User> user = userService.selectUserByUserName(userName);//查询用户名
        if (user.size() == 1) {//用户名存在
            msg = "userExist";
        } else {//用户不存在
            User newUser = new User(userName, password);
            if (userService.insertUser(newUser) == 1) {//注册成功
                msg = "registerSuccess";
            } else {//注册失败
                msg = "registerFalse";
            }
        }
        return msg;
    }

    @RequestMapping("/manageSys.do")
    public ModelAndView manageSys(String userName, String password) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("userName", userName);
        mv.addObject("modelName", "教学管理系统");
        mv.addObject("animate",0);
        mv.setViewName("manageSys");
        return mv;
    }
}
